var rootPath = getRootPath();
var isRightEmailNum = false;//是否是正确的邮箱
var click_flag = false;
var isPhone = isPhoneScreen();

$(function() {
	//1.手机重新输入时(获取焦点),去除所有的错误控制信息
	$("#email").focus(function() {
		hideMsg();
		isRightEmailNum = false;
		
		$("#sendCode").attr("disabled",true);
		$("#get_code").removeClass("active");
	});
	
	//2.邮箱地址码判断是否合法(移除焦点),如果合法,则可以获取验证码
	$("#email").blur(function(){
		if(!checkEmail($(this).val())){
			showErrMsg("请输入有效的邮箱地址");
			$("#sendCode").css({"background-color":"#bbbbbb","cursor":"default"});
			$("#get_code").css({"color":"#bbbbbb","cursor":"default"});
			
			$("#sendCode").attr("disabled",true);
			$("#get_code").removeClass("active");
			isRightEmailNum = false;
			return;
		} 

		$.ajax({
			type: "get",
	        url: rootPath + "/userFront/checkEmail",
	        async : false,
	        dataType: "json",
	        data: {
	        	"email": $("#email").val()
	        },
	        success: function (data){
	            if("0000" == data.returnCode && "true" == data.returnObject){
					isRightEmailNum = false;
	            } else {
	            	isRightEmailNum = true;
	            }
	            
	            if(isRightEmailNum) {
	            	hideMsg();
	            	$("#sendCode").css({"background-color":"#EA9792","cursor":"pointer"});
	    			$("#get_code").css({"color":"#B4282D","cursor":"pointer"});
	    			
	    			$("#sendCode").attr("disabled",false);
	    			$("#get_code").addClass("active");
	            } else {
	            	showErrMsg("该邮箱已绑定");
	            	$("#sendCode").css({"background-color":"#bbbbbb","cursor":"default"});
	    			$("#get_code").css({"color":"#bbbbbb","cursor":"default"});
	    			
	    			$("#sendCode").attr("disabled",true);
	    			$("#get_code").removeClass("active");
	            }
	        },error: function (jqXHR, textStatus, errorThrown) {
	        	layer.alert("error");
	        }
		});
	});
	
	
	
	//3.两个框都有值的情况，确定按钮显示正常
	$("#email,#code").bind('input propertychange',function() {
		if($("#email").val() == '' 
			|| $("#code").val() == '' || $("#code").val().length < 4 || !isRightEmailNum) {
			$("#save").css({"background":"#bbbbbb","cursor":"default"});
			$("#save").attr("disabled",true);
			click_flag = false;
		} else {
			$("#save").css({"background":"rgb(197,24,10)","cursor":"pointer"});
			$("#save").attr("disabled",false);
			click_flag = true;
		}
	});
	
	//4.发送验证码之后，显示倒计时60秒。
	if(isPhone) {
		$("#get_code").click(function() {
			if($(this).hasClass("active")) {
				getCheckCode($(this));
			}
		});		
	} else {
		$("#sendCode").click(function() {
			event.stopPropagation();
			getCheckCode($(this));
		});		
	}
	
	//5.确定按钮
	$("#save").click(save);
});

function showErrMsg(msg){
	if(msg != ''){
		$("#email_msg").text(msg);
		$(".msg").show();
	}
}

function hideMsg() {
	$("#email_msg").text("");
	$(".msg").hide();
}

//获得验证码接口 
function getCheckCode(obj) {
	if($("#email").val() == ''){
		showErrMsg("请输入邮箱地址");
		return;
	}
	
	if(!isRightEmailNum || !checkEmail($("#email").val())){
		showErrMsg("请输入正确的邮箱地址")
		return;
	}
	
	//倒计时中不能发送
	var countdown = $("#get_code").attr("countdown");
	if(countdown != '' && countdown > 0){
		return;
	}
	
	showErrMsg("正在发送,请稍等");
//	setTime($(this));
	setTime(obj);
	$.ajax({
		type: "get",
	    url: rootPath + "/userFront/sendCodeEmailValidate",
	    data: { 
	    	"email" : "'" + $("#email").val() + "'",
	    	"flag" : 2
	    },
	    dataType: "json",
	    success: function (data) {
	    	if("0000" == data.returnCode) {
	    		showErrMsg("发送成功");
	        } else {
	        	showErrMsg(data.returnMessage);
		    }
	    },
	    error:function (jqXHR, textStatus, errorThrown) {      
	    	showErrMsg("发送验证码失败,请检查您的网络.");
	    }
	});
}

//验证码倒计时
function setTime(obj) {
	var countdown = obj.attr("countdown");
	if(countdown == undefined || countdown == '' || countdown == 0) {
		countdown = 60;
	}
	var interval = setInterval(function() {
		countdown--;
		obj.attr("countdown",countdown);
		if(countdown == 0){
			if(isPhone) {
				obj.css({"color":"#B4282D","cursor":"pointer"});
				obj.addClass("active");
				obj.text("获取验证码");
			} else {
				obj.css({"background-color":"#EA9792","cursor":"pointer"});
				obj.attr('disabled',false); 
				obj.val("获取验证码");
			}
			window.clearInterval(interval);
		} else {
			if(isPhone) {
				obj.css({"color":"#bbbbbb","cursor":"default"});
				obj.text("重新发送(" + countdown + ")");
				obj.removeClass("active");
			} else {
				obj.css({"background-color":"#bbbbbb","cursor":"default"});
				obj.val("重新发送(" + countdown + ")");
				obj.attr('disabled',true);
			}
		}
	}, 1000);
}

//确定
function save() {
	if(!click_flag) return;
	
	$.ajax({
		url: rootPath + '/userFront/updateUserEmail',
        type: 'POST',
        contentType: 'application/json;charset=UTF-8',
        dataType: 'json',
        data: JSON.stringify($("#bindEmailForm").serializeJson()),
        success: function (data) {
        	if('0000' == data.returnCode) {
        		layer.msg("绑定成功",{icon:1,time:1000},function() {					
					parent.$("#content").prop("src", "securitySetting.html");
        		});
        	} else {
        		showErrMsg(data.returnMessage);
        	}
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error");
        }
    });
}